/**
 * 接口主函数
 * @param {Object} ctx HTTP上下文
 * @param {Object} db 数据管理器,如: { next: async function{}, ret: {} }
 * @return {Object} 执行结果
 */
async function main(ctx, db) {
	// 获取请求参数
	var req = ctx.request;
	var {
		query,
		body
	} = req;
	var types = [];
	var list = [];
	var plug = this.plugin();
	if (plug) {
		var options = plug.get_options();
		if (options) {
			var type_name = options.qa_type;
			var db1 = db.new('cms_article_type', 'type_id');
			var type = await db1.getObj({
				name: type_name
			});
			if (type) {
				db1.size = 0;
				types = await db1.get({
					father_id: type.type_id
				}, '`display` asc');
			}
			if (types.length) {
				var db2 = db.new('cms_article', 'article_id');
				db2.size = 0;
				for (var i = 0; i < types.length; i++) {
					var o = types[i];
					var lt = await db2.get({
						types_like: o.type_id
					});

					lt.map((item) => {
						var obj = list.getObj({
							article_id: item.article_id
						});
						if (!obj) {
							list.push(item);
						}
					});
				}
			}
		}
	}
	var ret = $.ret.list(list);
	ret.result.types = types;
	return ret;
}

exports.main = main;